package com.example.controller;

import com.example.domain.CultureProductCollect;
import com.example.service.CultureProductCollectService;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * (CultureProductCollect)表控制层
 *
 * @author lidongsheng
 * @since 2025-03-30 14:50:19
 */
@RestController
@RequestMapping("/front/cultureProductCollect")
public class CultureProductCollectController {
    /**
     * 服务对象
     */
    @Resource
    private CultureProductCollectService cultureProductCollectService;

    /**
     * 分页查询
     *
     * @param cultureProductCollect 筛选条件
     * @return 查询结果
     */
    @GetMapping
    public ResponseEntity<Page<CultureProductCollect>> queryByPage(
            CultureProductCollect cultureProductCollect,
            @RequestParam(defaultValue = "0") int page,
            @RequestParam(defaultValue = "10") int size,
            @RequestParam(defaultValue = "name,asc") String sort) {

        String[] sortParams = sort.split(",");
        Sort sortObj = Sort.by(Sort.Direction.fromString(sortParams[1]), sortParams[0]);
        PageRequest pageRequest = PageRequest.of(page, size, sortObj);
        return ResponseEntity.ok(this.cultureProductCollectService.queryByPage(cultureProductCollect, pageRequest));
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("{id}")
    public ResponseEntity<CultureProductCollect> queryById(@PathVariable("id") Integer id) {
        return ResponseEntity.ok(this.cultureProductCollectService.queryById(id));
    }

    /**
     * 新增数据
     *
     * @param cultureProductCollect 实体
     * @return 新增结果
     */
    @PostMapping
    public ResponseEntity<CultureProductCollect> add(CultureProductCollect cultureProductCollect) {
        return ResponseEntity.ok(this.cultureProductCollectService.insert(cultureProductCollect));
    }

    /**
     * 编辑数据
     *
     * @param cultureProductCollect 实体
     * @return 编辑结果
     */
    @PutMapping
    public ResponseEntity<CultureProductCollect> edit(CultureProductCollect cultureProductCollect) {
        return ResponseEntity.ok(this.cultureProductCollectService.update(cultureProductCollect));
    }

    /**
     * 删除数据
     *
     * @return 删除是否成功
     */
    @DeleteMapping
    public ResponseEntity<Boolean> deleteById(Integer productId,Integer userId) {
        return ResponseEntity.ok(this.cultureProductCollectService.deleteById(productId,userId));
    }
}

